define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'video.video/index' + location.search,
                    add_url: 'video.video/add',
                    edit_url: 'video.video/edit',
                    del_url: 'video.video/del',
                    multi_url: 'video.video/multi',
                    import_url: 'video.video/import',
                    table: 'video',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'cover_image', title: __('Cover_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'duration', title: __('Duration')},
                        {field: 'play_count', title: __('Play_count')},
                        {field: 'is_charge', title: __('Is_charge'), searchList: {"0":__('Is_charge 0'),"1":__('Is_charge 1')}, formatter: Table.api.formatter.normal},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'collection.title', title: __('Collection.title'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) { //自定义搜索条件
                    params.collection_id= Config.collection_id;
    
                    return params;
    
                }
            });
            

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // 监听视频URL变化获取时长
            $(document).on("change", "#c-video_url", function(){
                var video_url = $(this).val();
                if(video_url) {
                    var video = document.createElement('video');
                    video.src = 'https://oss.qiyunmanmankeji.com' + video_url;
                    
                    video.addEventListener('loadedmetadata', function(){
                        var duration = Math.round(video.duration);
                        $("#c-duration").val(duration);
                    });
                    $("#p-video_url").html('<li class="faupload-preview-item"><video style="width: 250px;height: 235px;" src="https://oss.qiyunmanmankeji.com' + video_url + '" controls></video></li>');
                    video.addEventListener('error', function(){
                        Toastr.error(__('Video duration get failed'));
                    });
                }
            });
            
            Controller.api.bindevent();
        },
        edit: function () {
            // 监听视频URL变化获取时长
            $(document).on("change", "#c-video_url", function(){
                var video_url = $(this).val();
                if(video_url) {
                    var video = document.createElement('video');
                    video.src = video_url;
                    
                    video.addEventListener('loadedmetadata', function(){
                        var duration = Math.round(video.duration);
                        $("#c-duration").val(duration);
                    });
                    $("#p-video_url").html('<li class="faupload-preview-item"><video style="width: 250px;height: 235px;" src="https://oss.qiyunmanmankeji.com' + video_url + '" controls></video></li>');
                    video.addEventListener('error', function(){
                        Toastr.error(__('Video duration get failed'));
                    });
                }
            });
            
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
